package com.woniu.atms.controller.pre;

import com.github.pagehelper.PageInfo;
import com.woniu.atms.service.UserTicketService;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import com.woniu.atms.vo.pre.ticket.PreTicketCheckedVO;
import com.woniu.atms.vo.pre.ticket.PreTicketUserIdVO;
import com.woniu.atms.vo.pre.ticket.PreUserTicketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * @author 张奇
 * @title: PreTicketController 用户车票管理
 * @projectName g5_atms
 * @description: TODO
 * @date 2022/1/17 15:09
 */
@Api(tags = "用户车票管理（前台）")
@RestController
@RequestMapping("/api/pre/v1/ticket")
public class PreTicketController {
    @Resource
    private UserTicketService userTicketService;

    /**
     * 查看用户所有车票信息 分页
     * @return
     */
    @ApiOperation(value = "通过用户id查查找自己购买的车票",notes = "参数为用户id和分页")
    @RequestMapping("/list")
    public ResponseResult queryList(@RequestBody @Valid PreTicketUserIdVO vo){
        PageInfo<PreUserTicketVO> preUserTicketVOS = userTicketService.queryAllByUserId(vo.getUserId(),vo.getPageInfoVO().getPageNum(),vo.getPageInfoVO().getPageSize());
        return new ResponseResult(Code.SUCCESS, "查询用户所有车票信息成功", preUserTicketVOS);
    }

    /**
     * 根据车票使用状态查询用户车票 分页
     * @param vo
     * @return
     */
    @ApiOperation(value = "通过用户id和车票使用状态查找自己购买的车票",notes = "参数为用户id和车票状态和分页")
    @RequestMapping("/ticket_use")
    public ResponseResult queryUseCheck(@RequestBody @Valid PreTicketCheckedVO vo){
        PageInfo<PreUserTicketVO> preUserTicketVOS = userTicketService.queryChecked(vo.getChecked(),vo.getUserId(),vo.getPageInfoVO().getPageNum(),vo.getPageInfoVO().getPageSize());
        return new ResponseResult(Code.SUCCESS, "查询用户未使用车票信息成功", preUserTicketVOS);
    }

    /**
     * 删除用户车票
     * @param id
     * @return
     */
    @ApiOperation(value = "通过用户id删除用户自己的车票",notes = "参数为用户id")
    @RequestMapping("/delete_ticket/{id}")
    public ResponseResult deleteTicket(@PathVariable("id") Integer id){
        //TODO 需要根据token携带的用户id判断是否是自己的车票
        boolean flag = userTicketService.deleteById(id);
        return new ResponseResult(Code.SUCCESS, "删除"+flag);
    }

    /**
     * 用户退票
     * @param id
     * @return
     */
    @ApiOperation(value = "通过用车票id退回用户自己的车票",notes = "参数为车票id")
    @RequestMapping("/refund/{id}")
    public ResponseResult reTicket(@PathVariable("id") Integer id){
        //TODO 需要根据token携带的用户id判断是否是自己的车票
        boolean flag = userTicketService.reTicket(id);
        return new ResponseResult(Code.SUCCESS, "退票"+flag);
    }

    /**
     * 通过订单编号退票
     * @param id
     * @return
     */
    @ApiOperation(value = "通过订单id退回用户自己的车票",notes = "参数为订单id")
    @RequestMapping("/refund_order/{id}")
    public ResponseResult reTicketByOrderId(@PathVariable("id") Integer id){
        //TODO 需要根据token携带的用户id判断是否是自己的车票

        boolean flag = userTicketService.reTicketByOrderId(id);
        return new ResponseResult(Code.SUCCESS, "退票"+flag,flag);
    }

    /**
     * 验票（乘客上车，使用车票）TODO 根据身份证号和车票编号和班次统一验票？
     * @param no
     * @return
     */
    @ApiOperation(value = "验票（车票使用）",notes = "参数为车票编号")
    @RequestMapping("/checked/{no}")
    public ResponseResult checked(@PathVariable("no") String no){
        //TODO 需要根据token携带的用户id判断是否是自己的车票
        boolean flag = userTicketService.checkedTicket(no);
        return new ResponseResult(Code.SUCCESS, "验票"+flag);
    }


}
